/**
 * 
 */
package cn.antia.cep.page.action;

import java.util.*;

import cn.antia.cep.busi.RoleService;
import cn.antia.cep.common.*;
import cn.antia.cep.common.pool.AntiaPoolHelper;
import cn.antia.cep.db.hibernate.bean.*;
import cn.antia.cwin.page.StrutsAction;

/**
 * @author sunkeei
 * @date Oct 31, 2007
 * @version 0.1
 * @time 
 */
public class RoleResourceAction extends StrutsAction {
	private AntiaPoolHelper antiaPoolHelper;
	public void setAntiaPoolHelper(AntiaPoolHelper antiaPool) {
		this.antiaPoolHelper = antiaPool;
	}
	private RoleService roleService;
	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	protected String action() throws AntiaException {
		String action = request.getParameter("action");
		if(action == null || action.equals("")){
			request.setAttribute("message", "Plealse appoint a action!");
			return "info";
		}
		if(action.equals("query")){
			return queryResource();
		}else if(action.equals("save")){
			return saveResource();
		}else{
			request.setAttribute("message", "Plealse appoint a valid action!this action is invalid:" + action);
			return "info";
		}
	}
	
	private String queryResource(){
		String roleId = request.getParameter("roleId");
		if(roleId == null || roleId.trim().length() < 1){
			request.setAttribute("message", "Please appoing a role!");
			return "info";
		}
		List resources = roleService.find("select resourceId from RoleResource where roleId = " + roleId);
		if(resources == null || resources.size() == 0){
			request.setAttribute("resources", "null");
		}else{
			StringBuffer sb = new StringBuffer();
			for(int i = 0;i<resources.size();i++){
				if(i == 0){
					sb.append(resources.get(i));
				}else{
					sb.append(",").append(resources.get(i));
				}
			}
			request.setAttribute("resources", sb.toString());
		}
		return "resource";
	}
	
	private String saveResource(){
		String idstring = request.getParameter("idstring");
		String roleId = request.getParameter("roleId");
		if(roleId == null || roleId.trim().length() < 1){
			request.setAttribute("message", "Plealse set a role..");
			return "info";
		}
		RoleResource rr = new RoleResource();
		int id = 0;
		rr.setRoleId(Integer.valueOf(roleId));
		roleService.remove(rr);
		if(idstring == null || idstring.trim().length() < 1){
			return queryResource();
		}
		String[] ids = idstring.split(",");
		String sid;
		for(int i = 0;i<ids.length;i++){
			sid = ids[i];
			if(sid.trim().equalsIgnoreCase("undefined")){
				List all = antiaPoolHelper.getAllResources();
				Resource resource;
				for(int j = 0;j<all.size();j++){
					resource = (Resource)all.get(j);
					if(resource.getCode().equals("root")){
						sid = resource.getId() + "";
					}
				}
			}
			try{
				id = Integer.parseInt(sid);
				rr = new RoleResource();
			}catch(Exception e){
				continue;
			}
			rr.setResourceId(new Integer(id));
			rr.setRoleId(Integer.valueOf(roleId));
			roleService.create(rr);
		}
		return queryResource();
	}
	
}
